﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LocAddrBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILocAddrDA LocAddrDA = DataAccess.CreateLocAddrDA();
        private static readonly ILocSiteDA LocSiteDA = DataAccess.CreateLocSiteDA();
        private PubParasInfo pubParasInfo;

        public LocAddrBLL()
        {
            this.pubParasInfo = null;
        }

        public LocAddrBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LocAddrBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LocAddrInfo> locAddrInfos = new List<LocAddrInfo>();
                LocAddrInfo locAddrInfo = null;
                foreach (string id in IDS)
                {
                    locAddrInfo = LocAddrDA.GetLocAddrByID(dataBase, tran, id, this.dataBaseID[0]);
                    locAddrInfos.Add(locAddrInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (LocAddrDA.DeleteLocAddr(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LocAddrInfo>(dataBase, tran, locAddrInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOC_ADDR", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return LocAddrDA.GetAllLocAddr(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            return LocAddrDA.GetLocAddrByID(id, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            LocAddrQueryEntity locAddrQuery = obj as LocAddrQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(locAddrQuery.LocAddrID))
            {
                sqlWhere.Add(" LOC_ADDR_ID " + locAddrQuery.LocAddrID);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.LocSiteID))
            {
                sqlWhere.Add(" LOC_SITE_ID " + locAddrQuery.LocSiteID);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.ContactName))
            {
                sqlWhere.Add(" CONTACT_NAME " + locAddrQuery.ContactName);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Telephone))
            {
                sqlWhere.Add(" TELEPHONE " + locAddrQuery.Telephone);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Email))
            {
                sqlWhere.Add(" EMAIL " + locAddrQuery.Email);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Fax))
            {
                sqlWhere.Add(" FAX " + locAddrQuery.Fax);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Province))
            {
                sqlWhere.Add(" PROVINCE " + locAddrQuery.Province);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.City))
            {
                sqlWhere.Add(" CITY " + locAddrQuery.City);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Zip))
            {
                sqlWhere.Add(" ZIP " + locAddrQuery.Zip);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Address))
            {
                sqlWhere.Add(" ADDRESS " + locAddrQuery.Address);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.IsWeekendWork))
            {
                sqlWhere.Add(" IS_WEEKEND_WORK " + locAddrQuery.IsWeekendWork);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.Remark))
            {
                sqlWhere.Add(" REMARK " + locAddrQuery.Remark);
            }
            if (!string.IsNullOrEmpty(locAddrQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + locAddrQuery.IsActive);
            }
            return LocAddrDA.GetLocAddrByQueryList(sqlWhere, locAddrQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string locAddrID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocAddrInfo locAddrInfo = obj as LocAddrInfo;
                if (LocAddrDA.CheckLocAddrIDUnique(dataBase, tran, locAddrInfo.LocAddrID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2550_001", this.pubParasInfo.UserLanguage, new List<string> { locAddrInfo.LocAddrID });
                }
                if (!LocSiteDA.CheckLocSiteIDUnique(dataBase, tran, locAddrInfo.LocSiteID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2540_002", this.pubParasInfo.UserLanguage, new List<string> { locAddrInfo.LocSiteID });
                }
                locAddrInfo.UpdatedBy = locAddrInfo.CreatedBy;
                locAddrInfo.CreatedDate = DateTime.Now;
                locAddrInfo.UpdatedDate = DateTime.Now;
                if (LocAddrDA.InsertLocAddr(dataBase, tran, locAddrInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocAddrInfo>(dataBase, tran, null, locAddrInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LOC_ADDR", this.dataBaseID[0]);
                    locAddrID = locAddrInfo.LocAddrID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locAddrID;
        }

        public string Update(object obj)
        {
            string locAddrID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                LocAddrInfo locAddrInfo = obj as LocAddrInfo;
                locAddrInfo.UpdatedDate = DateTime.Now;
                if (!LocSiteDA.CheckLocSiteIDUnique(dataBase, tran, locAddrInfo.LocSiteID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2540_002", this.pubParasInfo.UserLanguage, new List<string> { locAddrInfo.LocSiteID });
                }
                LocAddrInfo oldLocAddrInfo = LocAddrDA.GetLocAddrByID(dataBase, tran, locAddrInfo.LocAddrID, this.dataBaseID[0]);
                if (LocAddrDA.UpdateLocAddr(dataBase, tran, locAddrInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LocAddrInfo>(dataBase, tran, oldLocAddrInfo, locAddrInfo, AuditAction.Update, this.pubParasInfo.UserID, "LOC_ADDR", this.dataBaseID);
                    locAddrID = locAddrInfo.LocAddrID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return locAddrID;
        }
    }
}

